<template>
  <div style="margin: 10px">
    <Screen :category="category" @query="query" />
    <div style="margin-bottom: 10px">
      <el-button
        type="primary"
        @click="
          dialogVisible = true;
          title = '新增';
        "
      >添加文章</el-button>
    </div>
    <Tabel
      :columns="columns"
      :table-data="aricleList"
      :loading="loading"
      :page-index="queryForm"
    >
      <el-table-column label="图片" width="90">
        <template slot-scope="scope">
          <img :src="scope.row.description_image_path" class="articleImag">
        </template>
      </el-table-column>
      <!-- <el-table-column slot="handleState" label="设置热门">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.hot"
            :active-value="1"
            :inactive-value="0"
            active-color="#02538C"
            inactive-color="#B9B9B9"
            @change="changeSwitch(scope.row)"
          />
        </template>
      </el-table-column> -->
      <el-table-column
        slot="handleColumn"
        label="操作"
        width="250"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="
              ruleForm = { ...scope.row };
              dialogVisible = true;
              title = '编辑';
            "
          >信息编辑</el-button>
          <el-button
            size="mini"
            type="primary"
            @click="toArticleEdit(scope.row)"
          >文章编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelArticle(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </Tabel>
    <!-- 分页器 -->
    <Pagination
      :total="total"
      @pagination="pagination"
      @handleSizeChange="handleSizeChange"
    />
    <el-dialog
      :title="'文章信息' + title"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="ruleForm.title" placeholder="请输入文章标题" />
        </el-form-item>
        <el-form-item label="标签分类" prop="category_id">
          <el-select
            v-model="ruleForm.category_id"
            placeholder="请选择文章标签"
          >
            <el-option
              v-for="(item, index) of category"
              :key="index"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="页面分类" prop="webPageId">
          <el-select v-model="ruleForm.webPageId" placeholder="请选择文章类型">
            <el-option
              v-for="(item, index) of webPage"
              :key="index"
              :label="item.webPageName"
              :value="item.webPageId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否设置热门" prop="delivery">
          <el-switch
            v-model="ruleForm.hot"
            :active-value="1"
            :inactive-value="0"
            active-color="#02538C"
            inactive-color="#B9B9B9"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            v-show="title == '新增'"
            type="primary"
            @click="toArticleEdit('')"
          >确认</el-button>
          <el-button
            v-show="title == '编辑'"
            type="primary"
            @click="handleEdit"
          >编辑文章</el-button>
          <el-button @click="handleClose">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
const Screen = () => import('./components/screen')
const Tabel = () => import('@/components/Table')
const Pagination = () => import('@/components/Pagination')
import { article, changeSwitch, delArticle, articleEdit } from '@/api/article'
// import { getNowFormatTime } from '@/util.js'

// import Tabel from '@/components/Table';
export default {
  components: {
    Screen,
    Tabel,
    Pagination
  },
  data() {
    return {
      loading: false, // 加载
      columns: [
        {
          prop: 'title',
          label: '文章标题'
        },
        {
          prop: 'webPageName',
          label: '页面名称'
        },
        {
          prop: 'category_name',
          label: '标签'
        },
        {
          prop: '点赞数',
          label: '点赞数'
        },
        {
          prop: 'webPageName',
          label: '浏览数'
        },
        {
          prop: 'time',
          label: '时间',
          width: 180
        }
      ],
      aricleList: [], // 文章列表
      total: 0,
      category: [], // 标签分类
      queryForm: {
        size: 10,
        current: 1
      },
      dialogVisible: false, // 弹窗
      ruleForm: {}, // 文章
      title: '新增', // 弹窗标题
      webPage: [
        { webPageId: 0, webPageName: '普通文章' },
        { webPageId: 1, webPageName: '日记' }
      ],
      rules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        category_id: [
          { required: true, message: '请选择标签', trigger: 'blur' }
        ],
        webPageId: [
          { required: true, message: '请选择文章类型', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.query()
  },
  methods: {
    query(queryForm) {
      if (queryForm) {
        this.queryForm = queryForm
      }
      this.loading = true
      article(this.queryForm)
        .then((res) => {
          // res = res.data
          this.total = res.page.total
          this.aricleList = res.data
          this.category = res.category
        })
        .finally(() => {
          this.loading = false
        })
    },
    // 删除文章
    handleDelArticle({ id }) {
      this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delArticle({ id }).then((res) => {
            this.$message.success('删除成功')
            this.query()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 文章编辑
    toArticleEdit(row) {
      let url = '/articleEditor?'
      if (!row) {
        const {
          hot,
          category_id,
          title,
          webPageId
        } = this.ruleForm
        const webPageName = this.webPage.find(item => item.webPageId === webPageId)
        url += `title=${title}&category_id=${category_id}&hot=${hot}&webPageName=${webPageName}&webPageId=${webPageId}`
        this.$refs['ruleForm'].validate((valid) => {
          if (!valid) return
          this.$router.push(url)
        })
        return
      }
      url += 'id='
      this.$router.push(url + row.id)
    },
    // 分页事件
    pagination({ page: current, limit: size }) {
      this.queryForm.current = current
      this.query()
    },
    handleSizeChange({ page: current, limit: size }) {
      this.queryForm.size = size
      this.query()
    },
    // 热门修改
    changeSwitch(row) {
      changeSwitch({
        id: row.id,
        hot: row.hot
      }).then((res) => {
        // 判断是否设置成功
        if (res.code === 200) {
          this.$message.success(res.msg)
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
    // 选中行
    handleClose() {
      this.dialogVisible = false
      this.ruleForm = {}
      this.$refs['ruleForm'].resetFields()
    },
    handleEdit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (!valid) return
        // 将分类标签填入
        this.category.forEach((e) => {
          if (e.id === this.ruleForm.category_id) {
            this.ruleForm.category_name = e.name
            return
          }
        })
        articleEdit(this.ruleForm).then((res) => {
          this.$message.success('文章编辑成功')
          this.handleClose()
          this.query()
        })
      })
    }
  }
}
</script>
<style scoped>
.articleImag {
  width: 100%;
}
</style>
